/**
 * 收货入库confirmList.js
 * 版本号： v1.0.0
 */
var searchBtnId="";
var returnBtnId = "";
var cancelBtnId = "";
var putBtnId = "";
var saveBtnId = "";
$(function () {
    var purId = $('#purId_').val();
    //当前页面只查看未收货的物料
    var purdState = "1";
    //扩展datagrid行编辑
    $("#confirm_dataList").datagrid({
        pagination: true,
        fitColumns:true,
        fit:true,
        method: "POST",
        singleSelect: true,
        striped: true,
        border: false,
        pageSize: 10,
        pageList: [10, 50, 100],
        url: CONTEXT_PATH + '/purchaseApplyCheck/getContrastList.do?purId=' + purId + "&purdState=" + purdState,
        idField: 'purdId',
        columns: [
            [
                {field: 'origIndex', title: '序号', width: 30, fixed: true, align: 'center'},
                {field: 'newIndex', title: '序号', width: 30, fixed: true, hidden: true, align: 'center'},
                {field: 'materialName', title: '材料名称', width: 150, fixed: true, align: 'center'},
                {field: 'standardType', title: '规格型号', width: 150, fixed: true, align: 'center'},
                {field: 'pushDate', title: '入库时间', width: 180, fixed: true, hidden: true, align: 'center'},
                {field: 'supName', title: '供应商名称', width: 150, fixed: true, hidden: true},
                {field: 'purdNum', title: '本次请购量', width: 80, fixed: true, align: 'center'},
                {field: 'coId', title: '合同Id', width: 150, fixed: true, hidden: true},
                {field: 'maId', title: '物料Id', width: 150, fixed: true, hidden: true},
                {field: 'purType', title: '请购类型', width: 150, fixed: true, hidden: true},
                {field: 'unitPrice', title: '单价', width: 70, fixed: true, align: 'center'},
               /* {field: 'explain', title: '需求说明', width: 120, fixed: true, align: 'center'},
                {field: 'belongSystem', title: '所属系统', width: 120, fixed: true, align: 'center'},*/
                {field: 'leftCount', title: '合同剩余量', width: 80, fixed: true, align: 'center'},
                {field: 'meteringUnit', title: '计量单位', width: 70, fixed: true, align: 'center'},
                {field: 'purdDate', title: '到货时间', width: 150, fixed: true, hidden: true, align: 'center'},
                {field: 'pushPerson', title: '入库人员', width: 100, fixed: true,hidden: true, align: 'center'},
                {field: 'purdRemark', title: '物流单号备注', width: 120, fixed: true, align: 'center'},
                {field: 'pushRemark', title: '收货备注', width: 120, fixed: true, align: 'center', editor:{ type:'textbox', options:{}} },
                {field: 'purdState', title: '确认收货', width: 100, align: "center",
                    formatter: function (text) {
                        if (text == 1) return "<span style='color: red; font-weight: bold'>未收货</span>";
                        if (text == 2) return "<span style='color: green; font-weight: bold'>已收货</span>";
                    }, editor: {
                        type: 'combobox',
                        options: {
                            editable: false,
                            valueField: 'id',
                            textField: 'text',
                            method: 'get',
                            url: CONTEXT_PATH + "/purchaseSchedule/returnState.do",
                            required: true
                        }
                    }
                },
            ]
        ],
        onLoadSuccess: function (data) {
            var len = data.rows.length;
            for(var i = 0; i < len; i++){
                $("#confirm_dataList").datagrid("beginEdit", i);
            }
        }
    });


    //定义可操作按钮
    $("#confirm_Menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
    hideToolbar("confirmList");
});

/**
 * 隐藏指定的toolBar
 */
function hideToolbar(state) {
    //添加出库时
    if (state == "confirmList") {
        $('#' + searchBtnId).parent("td").show();
        $('#' + returnBtnId).parent("td").show();
        $('#' + putBtnId).parent("td").show();
        $('#' + cancelBtnId).parent("td").hide();
        $('#' + saveBtnId).parent("td").hide();
        $('#materialNameTd').show();
    }
    //生成出库单时
    if (state == "buildList") {
        $('#' + searchBtnId).parent("td").hide();
        $('#' + cancelBtnId).parent("td").show();
        $('#' + saveBtnId).parent("td").show();
        $('#' + returnBtnId).parent("td").hide();
        $('#' + putBtnId).parent("td").hide();
        $('#materialNameTd').hide();
    }

}


/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    searchBtnId = btnOptions[i].value.meId;
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            search();
                        }
                    });
                }
                //取消按钮
                if ("cancelBtn" == btnOptions[i].key) {
                    cancelBtnId = btnOptions[i].value.meId;
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            cancel();

                        }
                    });
                }
                //返回页面按钮
                if ("returnBtn" == btnOptions[i].key) {
                    returnBtnId = btnOptions[i].value.meId;
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            window.location.href = CONTEXT_PATH + "/purchaseSchedule/index.do"
                        }
                    });
                }
                //收货入库按钮
                if ("putBtn" == btnOptions[i].key) {
                    putBtnId = btnOptions[i].value.meId;
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            buildPull();
                        }
                    });
                }
                //保存入库单
                if ("saveBtn" == btnOptions[i].key) {
                    saveBtnId = btnOptions[i].value.meId;
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            confirm(this);
                        }
                    });
                }
            }
        }
        return toolbar;
    }
}

/**
 * 查询
 */
function search() {
    //刷新页码
    refreshPageNumber("confirm_dataList");
    $("#confirm_dataList").datagrid("reload", {
        //查询数据
        materialName: $("#materialName").textbox('getValue')
    });
    $("#confirm_dataList").datagrid("clearSelections");
}

/**
 * 收货入库
 */
//是否存在收货的数据
var dataExist = "false";
function buildPull() {
    var rows = $('#confirm_dataList').datagrid("getRows");
    for(var i = 0; i < rows.length; i++){
        $('#confirm_dataList').datagrid('endEdit', i);
    }
    //显示入库单
    var columns = $('#confirm_dataList').datagrid("options").columns;
    var pullList = $('#confirm_dataList').datagrid('getChanges', "updated");
    if (pullList.length > 0) {
        //获取当前时间
        var pushDate = getFormatDate();
        //存储需显示的行标
        var mIndex = new Map();
        for (var i = 0; i < pullList.length; i++) {
            var purdState =pullList[i].purdState;
            var index = $("#confirm_dataList").datagrid("getRowIndex", pullList[i]);
            mIndex.set(index, purdState);
        }
        //显示新序列及入库时间列
        $('#confirm_dataList').datagrid('showColumn', 'newIndex');
        $('#confirm_dataList').datagrid('showColumn', 'purdDate');
        $('#confirm_dataList').datagrid('showColumn', 'supName');
        //隐藏原序列及不需要的列
        $('#confirm_dataList').datagrid('hideColumn', 'origIndex');
        $('#confirm_dataList').datagrid('hideColumn', 'unitPrice');
       /* $('#confirm_dataList').datagrid('hideColumn', 'explain');
        $('#confirm_dataList').datagrid('hideColumn', 'belongSystem');*/
        $('#confirm_dataList').datagrid('hideColumn', 'leftCount');
        $('#confirm_dataList').datagrid('hideColumn', 'purdRemark');
        $('#confirm_dataList').datagrid('hideColumn', 'pushRemark');
        //设置列不允许编辑pullPerson
        var pullPerson = $("#confirm_dataList").datagrid('getColumnOption', 'pushPerson');
        pullPerson.editor = {};
        //隐藏相应按钮
        hideToolbar("buildList");
        var totalRow = $("#confirm_dataList").datagrid('getRows').length;
        //新序列
        var newIndex = 0;
        for (var j = 0; j < totalRow; j++) {
            var showIndex = mIndex.get(j);
            if (typeof (showIndex) != "undefined") { //显示行
                if (showIndex==2) {
                    dataExist = "true";
                    //创建新的序列
                    pullList[j][columns[0][1].field] = newIndex + 1;
                    //创建入库时间
                    pullList[j][columns[0][4].field] = pushDate;
                    //创建到货时间
                    pullList[j][columns[0][13].field] = pushDate;
                    $('#confirm_dataList').datagrid('refreshRow', j);
                    newIndex++;
                }else {
                    $('#datagrid-row-r1-2-' + j).hide();
                }
            } else {  //隐藏行
                $('#datagrid-row-r1-2-' + j).hide();
            }
        }
    }
}

/**
 * 取消返回确认收货页面
 */
function cancel() {
    dataExist = "false";
    //显示相应按钮
    hideToolbar("confirmList");
    //隐藏新序列及出库时间列
    $('#confirm_dataList').datagrid('hideColumn', 'newIndex');
    $('#confirm_dataList').datagrid('hideColumn', 'purdDate');
    $('#confirm_dataList').datagrid('hideColumn', 'supName');
    //显示原序列及其他字段
    $('#confirm_dataList').datagrid('showColumn', 'origIndex');
    $('#confirm_dataList').datagrid('showColumn', 'purdNum');
    $('#confirm_dataList').datagrid('showColumn', 'unitPrice');
    /*$('#confirm_dataList').datagrid('showColumn', 'explain');
    $('#confirm_dataList').datagrid('showColumn', 'belongSystem');*/
    $('#confirm_dataList').datagrid('showColumn', 'leftCount');
    $('#confirm_dataList').datagrid('showColumn', 'purdRemark');
    $('#confirm_dataList').datagrid('showColumn', 'pushRemark');
    $('#confirm_dataList').datagrid('showColumn', 'purdState');
    var rows = $('#confirm_dataList').datagrid("getRows");
    for(var i = 0; i < rows.length; i++){
        $("#confirm_dataList").datagrid("beginEdit", i);
    }
    var totalRow = $("#confirm_dataList").datagrid('getRows').length;
    for (var j = 0; j < totalRow; j++) {
        $('#datagrid-row-r1-2-' + j).show();
    }
}

/**
 * 保存入库单
 */
function confirm(objBtn) {
    if(dataExist=="false"){
        $.messager.show({ title:"系统提示", msg: "无收货数据，请完善后再进行收货！", timeout:3000, showType:"slide" });
        cancel();
        return;
    }
    $.messager.confirm("系统提示", "您确定保存入库单？", function(r) {
        if (r) {
            $(objBtn).linkbutton("disable");
            var arr = $('#confirm_dataList').datagrid('getChanges', 'updated');
            var purchaseDetailArr = [];
            for (var i = 0; i < arr.length; i++) {
                var purdState=arr[i].purdState;
                if (purdState==2) {
                    var obj = {
                        'purdId': arr[i].purdId ? arr[i].purdId : '',
                        'purId': arr[i].purId ? arr[i].purId : '',
                        'purdState': arr[i].purdState ? arr[i].purdState : '',
                        'purdDate': arr[i].purdDate ? arr[i].purdDate : '',
                        'maId': arr[i].maId ? arr[i].maId : '',
                        'coId': arr[i].coId ? arr[i].coId : '',
                        'purType': arr[i].purType ? arr[i].purType : '',
                        'pushDate': arr[i].pushDate ? arr[i].pushDate : '',
                       /* 'purdRemark': arr[i].purdRemark ? arr[i].purdRemark : '',*/
                        'pushRemark': arr[i].pushRemark ? arr[i].pushRemark : '',
                        'purdNum':arr[i].purdNum
                    };
                    purchaseDetailArr.push(obj)
                }
            }
            $.ajax({
                url: CONTEXT_PATH + "/purchaseSchedule/confirm.do",
                type: "post",
                data: JSON.stringify(purchaseDetailArr),
                contentType: "application/json",
                dataType: "json",
                success: function (data) {
                    var url = CONTEXT_PATH + "/purchaseSchedule/index.do";
                    saveJump(data,url);
                },
                error:function(){
                    $.messager.show({ title:"系统提示", msg: "保存入库单失败！", timeout:3000, showType:"slide" });
                }
            })
        }
    })

}



